package com.paul.sys.controller.back;

import com.paul.sys.model.RoadParkInfo;
import com.paul.sys.service.RoadParkInfoService;
import com.paul.sys.tools.PageData;
import com.paul.sys.util.ParamUtil;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by �?aaaaaaa on 2015/10/27.
 */
@Controller
@RequestMapping("/back/roadpark/")
public class RoadParkController {


    private static Logger logger = Logger.getLogger(RoadParkController.class);
    @Autowired
    private RoadParkInfoService roadParkInfoService;


    @RequestMapping("ismenu/roadpark")
    public ModelAndView role() throws Exception {
        ModelAndView view = new ModelAndView("back/roadPark/roadPark-list_2");

        Integer total = roadParkInfoService.countAll(new HashMap());
        view.addObject("total", total);
        return view;
    }


    @RequestMapping("edit")
    public ModelAndView Modify(HttpServletRequest request) throws Exception {
        ModelAndView view = new ModelAndView("back/roadPark/roadPark-edit");
        Integer id = Integer.parseInt(request.getParameter("id"));
        RoadParkInfo entity = roadParkInfoService.selectByPrimaryKey(id);
        view.addObject("roadpark", entity);
        return view;
    }


    @ResponseBody
    @RequestMapping("getCount")
    public Integer countSelect(HttpServletRequest request) throws Exception {
        Integer totalselect = 0;
        try {
            ParamUtil util = new ParamUtil(request);
            Map<String, String> param = util.getParamMap();
            totalselect = roadParkInfoService.countAll(param);
        } catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
        return totalselect;
    }

    @ResponseBody
    @RequestMapping("roadPark-Map")
    public ModelAndView localMap(String lng, String lat) throws Exception {
        ModelAndView view = new ModelAndView("back/roadPark/roadPark-Map");
        view.addObject("lng", lng);
        view.addObject("lat", lat);
        return view;
    }


    @ResponseBody
    @RequestMapping("list")
    public Map<String, Object> listPtstaff(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            ParamUtil util = new ParamUtil(request);
            Map<String, String> param = util.getParamMap();
            PageData pageData = roadParkInfoService.listByPage(param);
            result.put("totalRows", pageData.getPageBean().getTotalNum());
            result.put("curPage", param.get("curPage"));
            result.put("data", pageData.getListData());
            result.put("success", true);
        } catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        }

        return result;
    }

    @ResponseBody
    @RequestMapping("getRoadPark")
    public Map<String, Object> getRoadPark(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            Integer id = Integer.parseInt(request.getParameter("id"));
            RoadParkInfo entity = roadParkInfoService.selectByPrimaryKey(id);
            result.put("data", entity);
        } catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    @ResponseBody
    @RequestMapping("updateRoadPark")
    public Map<String, Object> setRoadPark(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            ParamUtil util = new ParamUtil(request);
            Map<String, String> param = util.getParamMap();
            JSONObject jsonObject = JSONObject.fromObject(param);
            RoadParkInfo roadParkInfo = (RoadParkInfo) JSONObject.toBean(jsonObject, RoadParkInfo.class);
            roadParkInfoService.updateByRecord(roadParkInfo);
        } catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    @ResponseBody
    @RequestMapping("updateCoordinate")
    public Map<String, Object> setCoordinate(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            ParamUtil util = new ParamUtil(request);
            Map<String, String> param = util.getParamMap();
            JSONObject jsonObject = JSONObject.fromObject(param);
            RoadParkInfo roadParkInfo = (RoadParkInfo) JSONObject.toBean(jsonObject, RoadParkInfo.class);
            roadParkInfoService.updateById(roadParkInfo);
        } catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

}
